<?php
/**
 * 数据删除操作
 * @author Ice <iceinto@mallog.com.cn>
 */
class DeleteAction extends LoginAction
{
	public function _initialize ()
	{
		parent::_initialize();
		$user = $this->__get('userInfo');
	}
	public function index ()
	{
		$this->display();
	}
	public function mall ()
	{
	}
	/**
	 * 资讯高级管理
	 * @author Ice <iceinto@mallog.com.cn>
	 */
	public function news ()
	{
		$BrandNewsModel = D('BrandNews');
		$data = $BrandNewsModel->getManagedNews();
		$this->assign('news', $data['data']);
		$this->assign('page', $data['page']);
		$this->display();
	}
	/**
	 * 资讯详细
	 * @author Ice <iceinto@mallog.com.cn>
	 */
	public function newDetail ()
	{
		$id = intval($this->_get('id'));
		$BrandNewsModel = M('BrandNews');
		$new = $BrandNewsModel->find($id);
		if (! $new) {
			$this->error('资讯信息错误或不存在！');
		}
		if (isPost()) {
			$isDel = intval(getPost('isDel'));
			if ($isDel) {
				//执行删除
				if ($BrandNewsModel->delete($id)) {
					//返回列表界面
					$this->redirect('Delete/news');
				} else {
					$this->error('资讯删除失败！！');
				}
			} else {
				$mall = intval(getPost('market'));
				$floor = intval(getPost('floor'));
				$brand = intval(getPost('brand'));
				if ($mall && $floor && $brand) {
					$data = array();
					$data['mid'] = $mall;
					$data['fid'] = $floor;
					$data['bid'] = $brand;
					$map = array();
					$map['id'] = $id;
					if ($BrandNewsModel->where($map)->save($data)) {
						$new = $BrandNewsModel->find($id);
					} else {
						$this->error('资讯所属调整失败！');
					}
				} else {
					$this->error('商场、楼层和品牌信息必须选择！');
				}
			}
		}
		//获取所属信息
		$MarketModel = M('Market');
		$FloorModel = M('Floor');
		$BrandModel = M('Brand');
		$mall = $MarketModel->find($new['mid']);
		$floor = $FloorModel->find($new['fid']);
		$brand = $BrandModel->find($new['bid']);
		$market = $MarketModel->select();
		$this->assign('market', $market);
		$this->assign('mall', $mall);
		$this->assign('floor', $floor);
		$this->assign('brand', $brand);
		$this->assign('new', $new);
		$this->display();
	}

	/**
	 * 楼层品牌管理
	 * @author Ice <iceinto@mallog.com.cn>
	 */
	public function fb(){
		$floors = array();
		$brands = array();
		$map = array();
		$deleteNav = array();
		$mid = intval($this->_get('mid'));
		$fid = intval($this->_get('fid'));
		$bid = intval($this->_get('bid'));
		$MarketModel = M('Market');
		$FloorModel = M('Floor');
		$malls = $MarketModel->select();
		if($mid){
			$map['id'] = $mid;
			$mall = $MarketModel->field('id,name')->where($map)->find();
			if($mall){
				$deleteNav['mall'] = $mall;
			}else{
				$this->error('商场信息不存在！');
			}
			unset($map);
			$map['mid'] = $mid;
			$floors = $FloorModel->where($map)->select();
			if($fid){
				//获取楼层信息
				unset($map);
				$map['id'] = $fid;
				$floor = $FloorModel->field('sain as id, name')->where($map)->find();
				if($floor){
					$deleteNav['floor'] = $floor;
				}else{
					$this->error('楼层信息不存在！');
				}
				//获取品牌信息
				unset($map);
				$map['mid'] = $mid;
				$map['fid'] = $fid;
				$brands = M('FloorBrand')
				->field('m_floor_brand.*, m_brand.name')
				->join('INNER JOIN m_brand ON m_floor_brand.bid = m_brand.id')
				->where($map)
				->select();
				if($bid){
					unset($map);
					$map['id'] = $bid;
					$brand = M('Brand')->field('id,name')->where($map)->find();
					if($brand){
						$deleteNav['brand'] = $brand;
						
					}else{
						$this->error('品牌信息不存在！');
					}
				}
			}
		}
		if($mid && $fid && $bid){
			//获取品牌资讯和商品信息
			//先获取数量
			unset($map);
			$map['mid'] = $mid;
			$map['fid'] = $fid;
			$map['bid'] = $bid;
			$newCount = M('BrandNews')->where($map)->count();
			unset($map);
			$map['mid'] = $mid;
			$map['fid'] = $fid;
			$map['bid'] = $bid;
			$goodCount = M('FloorBrandGood')->where($map)->count();
			$this->assign('newCount', $newCount);
			$this->assign('goodCount', $goodCount);
		}

		$this->assign('deleteNav', $deleteNav);
		$this->assign('mid', $mid);
		$this->assign('fid', $fid);
		$this->assign('bid', $bid);
		$this->assign('malls', $malls);
		$this->assign('floors', $floors);
		$this->assign('brands', $brands);
		$this->display();
	}

	/**
	 * 商场移动柜台
	 */
	public function moveCounter(){
		$mid = intval($this->_get('mid'));
		$fid = intval($this->_get('fid'));
		$bid = intval($this->_get('bid'));
		$map = array();
		if(isPost()){
			$pmid = intval(getPost('mid'));
			$pfid = intval(getPost('fid'));
			if(($pfid == $fid) && ($pmid == $mid)){
				$this->error('没有移动楼层啊！请重新选择！');
			}else{
				$data = array();
				//执行修改
				//修改楼层所属信息
				$map['mid'] = $mid;
				$map['mid'] = $fid;
				$map['bid'] = $bid;
				$data['mid'] = $pmid;
				$data['fid'] = $pfid;
				M('FloorBrand')->where($map)->save($data);
				//修改品牌资讯
				unset($map);
				unset($data);
				$map['mid'] = $mid;
				$map['fid'] = $fid;
				$map['bid'] = $bid;
				$data['mid'] = $pmid;
				$data['fid'] = $pfid;
				M('BrandNews')->where($map)->save($data);
				//修改品牌商品信息
				unset($map);
				unset($data);
				$map['mid'] = $mid;
				$map['fid'] = $fid;
				$map['bid'] = $bid;
				$data['mid'] = $pmid;
				$data['fid'] = $pfid;
				M('FloorBrandGood')->where($map)->save($data);
				unset($map);
				unset($data);
				$this->assign("jumpUrl", U('fb').'?mid='.$pmid.'&fid='.$pfid.'&bid='.$bid);
				$this->success('移动完毕，请尽快修改品牌楼层类型！');
			}
		}
		if($mid && $fid && $bid){
			unset($map);
			$mall = M('Market')->find($mid);
			$map['mid'] = $mid;
			$map['id'] = $fid;
			$floor = M('Floor')->where($map)->find();
			unset($map);
			$map['mid'] = $mid;
			$map['fid'] = $fid;
			$map['bid'] = $bid;
			$brand = M('FloorBrand')->where($map)->find();
			unset($map);
			if($mall && $floor && $brand){
				$brand = M('Brand')->find($bid);
				$malls = M('Market')->select();
				$this->assign('mid', $mid);
				$this->assign('fid', $fid);
				$this->assign('bid', $bid);
				$this->assign('malls', $malls);
				$this->assign('mall', $mall);
				$this->assign('floor', $floor);
				$this->assign('brand', $brand);
			}else{
				$this->error('数据获取错误！');
			}
		}else{
			$this->error('信息提供错误！');
		}
		$this->display();
	}
}
?>